//
//  SubjectRightVC.m
//  Cat
//
//  Created by dllo on 15/12/2.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "SubjectRightVC.h"
#import "MyFlowLayout.h"
#import "CYHSubjectCell.h"
#import "ToolCoreData.h"
#import "CYHSubjectDrawVC.h"

@interface SubjectRightVC ()<UICollectionViewDataSource, UICollectionViewDelegate, MyFlowLayoutDelegate>

@property (nonatomic, strong) UICollectionView *RightCollectionView;
@property (nonatomic, strong) NSMutableArray *picArr;

@end

@implementation SubjectRightVC

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.picArr = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createView];
    [self createData];
}

- (void)createView {
    MyFlowLayout *flowLayout = [[MyFlowLayout alloc] init];
    flowLayout.MyDelegate = self;
    
    self.RightCollectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout];
    [self.view addSubview:self.RightCollectionView];
    self.RightCollectionView.delegate = self;
    self.RightCollectionView.dataSource = self;
    self.RightCollectionView.backgroundColor = [UIColor colorWithRed:245.0 / 255.0 green:245.0 / 255.0 blue:245.0 / 255.0 alpha:1];
     
    [self.RightCollectionView registerClass:[CYHSubjectCell class] forCellWithReuseIdentifier:@"RightReuse"];
    
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    [self.RightCollectionView addGestureRecognizer:longPress];
}

- (void)longPress:(UIGestureRecognizer *)longPress {
    
    CGPoint pointTouch = [longPress locationInView:self.RightCollectionView];
    if (longPress.state == UIGestureRecognizerStateBegan) {
        NSIndexPath *indexPath = [self.RightCollectionView indexPathForItemAtPoint:pointTouch];
        if (indexPath == nil) {
            NSLog(@"空");
        }else {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确定要删除么" message:@"" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
//                CYHSubjectCell *cell = (CYHSubjectCell *)[self.RightCollectionView cellForItemAtIndexPath:indexPath];
                [self.picArr removeObjectAtIndex:indexPath.row];
                ToolCoreData *toolCoreData = [ToolCoreData defaults];
                [toolCoreData deletePicture:[toolCoreData takePicture][indexPath.row]];
                [self.RightCollectionView reloadData];
            
            }];
            
            UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
            }];
            [alert addAction:okAction];
            [alert addAction:cancleAction];
            [self presentViewController:alert animated:YES completion:nil];
            
        }
    }
}
- (void)createData {
    ToolCoreData *toolCoreData = [ToolCoreData defaults];
    self.picArr = [NSMutableArray arrayWithArray:[toolCoreData takePicture]];
}

#pragma mark - collectionView
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.picArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CYHSubjectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RightReuse" forIndexPath:indexPath];
    
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:self.picArr[indexPath.item]] placeholderImage:[UIImage imageNamed:@"ooopic_1445565008@2x.png"]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    CYHSubjectDrawVC *drawVC = [[CYHSubjectDrawVC alloc] init];
    drawVC.PicUrl = self.picArr[indexPath.item];
    [self.navigationController pushViewController:drawVC animated:YES];
}

// 行高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(VIEW_WIDTH / 2 - 10, arc4random() % (20) + 200);;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
